package com.human.server.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.human.server.entity.AssessmentFills;
import com.human.server.entity.KpiFillDetails;
import com.human.server.model.R;
import com.human.server.model.request.AssessmentFillsRequest;
import com.human.server.model.response.AssessmentFillDetail;
import com.human.server.service.impl.AssessmentFillsServiceImpl;
import com.human.server.service.impl.KpiFillDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 考核填写主表 前端控制器
 * </p>
 *
 * @author zooy
 * @since 2025-09-11
 */
@RestController
@RequestMapping("/human/assessmentFills")
public class AssessmentFillsController {

    @Autowired
    private AssessmentFillsServiceImpl assessmentFillsService;

    @Autowired
    private KpiFillDetailsServiceImpl kpiFillDetailsService;

    /**
     * 新增考核填写主表
     *
     * @param assessmentFills 考核填写主表
     * @return 新增结果
     */
    @PostMapping("/add")
    public R add(@RequestBody AssessmentFills assessmentFills) {
        return R.success(assessmentFillsService.save(assessmentFills));
    }

    /**
     * 新增考核填写主表
     *
     * @param assessmentFills 考核填写主表
     * @return 新增结果
     */
    @PostMapping("/update")
    public R update(@RequestBody AssessmentFills assessmentFills) {
        return R.success(assessmentFillsService.updateById(assessmentFills));
    }

    /**
     * 新增考核填写主表
     *
     * @param assessmentFills 考核填写主表
     * @return 新增结果
     */
    @PostMapping("/delete")
    public R delete(@RequestBody AssessmentFills assessmentFills) {
        return R.success(assessmentFillsService.removeById(assessmentFills));
    }

    /**
     * 新增考核填写主表
     *
     * @param assessmentFills 考核填写主表
     * @return 新增结果
     */
    @PostMapping("/detail")
    public R detail(@RequestBody AssessmentFills assessmentFills) {

        AssessmentFills fills = assessmentFillsService.getById(assessmentFills);
        // 查询kpi
        List<KpiFillDetails> kpiFillDetailsList = kpiFillDetailsService.list(new LambdaQueryWrapper<KpiFillDetails>()
                .eq(KpiFillDetails::getFillId, fills.getId()));

        AssessmentFillDetail assessmentFillDetail = new AssessmentFillDetail();
        assessmentFillDetail.setAssessmentFills(fills);
        assessmentFillDetail.setKpiFillDetailsList(kpiFillDetailsList);
        return R.success(assessmentFillDetail);
    }

    /**
     * 新增考核填写主表
     *
     * @param assessmentFills 考核填写主表
     * @return 新增结果
     */
    @PostMapping("/list")
    public R list(@RequestBody AssessmentFills assessmentFills) {
        return R.success(assessmentFillsService.list(new LambdaQueryWrapper<AssessmentFills>()
                .eq(assessmentFills.getEmployeeId() != null, AssessmentFills::getEmployeeId, assessmentFills.getEmployeeId())
                .orderByDesc(AssessmentFills::getCreateTime)
        ));
    }

    /**
     * 新增考核填写主表
     *
     * @param assessmentFillsRequest 考核填写主表
     * @return 新增结果
     */
    @PostMapping("/page")
    public R page(@RequestBody AssessmentFillsRequest assessmentFillsRequest) {

        return R.success(assessmentFillsService.page(assessmentFillsRequest,
                new LambdaQueryWrapper<AssessmentFills>()
                        .eq(assessmentFillsRequest.getAssessmentId() != null, AssessmentFills::getAssessmentId, assessmentFillsRequest.getAssessmentId())
                        .eq(assessmentFillsRequest.getDepartmentId() != null, AssessmentFills::getDepartmentId, assessmentFillsRequest.getDepartmentId())
                        .like(StrUtil.isNotBlank(assessmentFillsRequest.getEmployeeName()), AssessmentFills::getEmployeeName, assessmentFillsRequest.getEmployeeName())
                        .like(StrUtil.isNotBlank(assessmentFillsRequest.getFillStatus()), AssessmentFills::getFillStatus, assessmentFillsRequest.getFillStatus())
                        .orderByDesc(AssessmentFills::getCreateTime)));
    }

    /**
     * 新增考核填写主表
     *
     * @param assessmentFillsRequest 考核填写主表
     * @return 新增结果
     */
    @PostMapping("/countFillStatusByDepartment")
    public R countFillStatusByDepartment(@RequestBody AssessmentFillsRequest assessmentFillsRequest) {

        return R.success(assessmentFillsService.countFillStatusByDepartment(assessmentFillsRequest.getAssessmentId()));
    }

}
